Completed
Push — master ( fdb97b...6ab70a )
by Andres
39s
created

upgrades.js ➔ ... ➔ ct.buyAll   C

Complexity

Conditions 7
Paths 8

Size

Total Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 7
c 1
b 0
f 0
nc 8
dl 0
loc 27
rs 6.7272
nop 2
1
/**
2
 upgrades
3
 Component that handles upgrades.
4
5
 @namespace Components
6
 */
7
'use strict';
8
9
angular.module('game').component('upgrades', {
10
  templateUrl: 'views/upgrades.html',
11
  controller: ['state', 'visibility', 'upgrade', 'data', upgrades],
12
  controllerAs: 'ct'
13
});
14
15
function upgrades(state, visibility, upgradeService, data) {
16
  let ct = this;
17
  ct.state = state;
18
  ct.data = data;
19
  ct.upgradeService = upgradeService;
20
  let sortFunc = upgradeService.sortFunctions(data.upgrades);
21
22
  // tries to buy all the upgrades it can, starting from the cheapest
23
  ct.buyAll = function (slot, player) {
24
    let currency = data.elements[slot.element].main;
25
    let cheapest;
26
    let cheapestPrice;
27
    do{
28
      cheapest = null;
29
      cheapestPrice = Infinity;
0 ignored issues
show
Comprehensibility Best Practice introduced by
You seem to be aliasing the built-in name Infinity as cheapestPrice. This makes your code very difficult to follow, consider using the built-in name directly.
Loading history...
30
      for(let up of ct.visibleUpgrades(slot, player)){
31
        let price = data.upgrades[up].price;
32
        if(!slot.upgrades[up] &&
33
          price <= state.player.resources[currency].number){
34
          if(price < cheapestPrice){
35
            cheapest = up;
36
            cheapestPrice = price;
37
          }
38
        }
39
      }
40
      if(cheapest){
41
        upgradeService.buyUpgrade(player,
42
          slot.upgrades,
43
          data.upgrades[cheapest],
44
          cheapest,
45
          cheapestPrice,
46
          currency);
47
      }
48
    }while(cheapest);
49
  };
50
51
  ct.buyUpgrade = function (name, slot, player) {
52
    let price = data.upgrades[name].price;
53
    let currency = data.elements[slot.element].main;
54
    upgradeService.buyUpgrade(player,
55
      slot.upgrades,
56
      data.upgrades[name],
57
      name,
58
      price,
59
      currency);
60
  };
61
62
  ct.visibleUpgrades = function(slot, player) {
63
    return visibility.visible(data.upgrades, isBasicUpgradeVisible, slot, sortFunc[player.options.sortIndex], player);
64
  };
65
66
  function isBasicUpgradeVisible(name, slot, player) {
67
    let isVisible = visibility.isUpgradeVisible(name, slot, data.upgrades[name], player);
68
    return isVisible && (!player.options.hideBought || !slot.upgrades[name]);
69
  }
70
71
  ct.visibleGlobalUpgrades = function() {
72
    return visibility.visible(data.global_upgrades, upgradeService.filterByTag('global'));
73
  };
74
}
75